﻿//
// EntryView.xaml.cs
//
// Authors:
//    Anders Gade Nørskov <09549@iha.dk
//    Lars Rønde Hansen <09548@iha.dk>
//    Claus Jørgensen <10229@iha.dk>
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Navigation;
using System.Windows.Controls.Primitives;
using System.Windows.Media.Animation;
using System.Windows.Threading;
using GalaSoft.MvvmLight.Messaging;
using StructureMap;

namespace RSD.View
{
    using Assets;
    using Data;
    using ViewModel;
    using ViewModel.Messages;

    /// <summary>
    /// Interaction logic for EntryView.xaml
    /// </summary>
    public partial class EntryView : Window
    {
        private DispatcherTimer timer;

        public EntryView()
        {
            InitializeComponent();

            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(5);
            timer.Tick += (o, e) => HidePopup();

            Messenger.Default.Register<ExceptionMessage>(this,
                msg =>
                {
                    Dispatcher.Invoke(new Action(() =>
                    {
                        if (msg.Exception.InnerException != null)
                        {
                            ShowMessage(msg.Exception.InnerException.Message);
                        }
                        else
                        {
                            ShowMessage(msg.Exception.Message);
                        }
                    }));
                });

            Messenger.Default.Register<InvalidCredentialsMessage>(this,
                msg =>
                {
                    Dispatcher.Invoke(new Action(() =>
                    {
                        ShowMessage("Invalid Username or Password.");
                    }));
                });

            Messenger.Default.Register<UserLoggedOnMessage>(this,
                msg =>
                {
                    Dispatcher.Invoke(new Action(() =>
                    {
                        NotificationPopup.Visibility = Visibility.Collapsed;
                        ViewFrame.Navigate(ObjectFactory.GetInstance<MainView>(), msg.User);
                        ViewFrame.LoadCompleted += ViewFrame_LoadCompleted;
                    }));
                });
        }

        private void ViewFrame_LoadCompleted(object sender, NavigationEventArgs e)
        {
            Messenger.Default.Send(new MainViewLoadedMessage((User)e.ExtraData));
        }

        private void NotificationPopup_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            HidePopup();
        }

        private void HidePopup()
        {
            timer.Stop();

            var hidePopup = (Resources["HidePopup"] as Storyboard);
            hidePopup.Completed += new EventHandler(HidePopup_Completed);
            hidePopup.Begin();
        }

        private void HidePopup_Completed(object sender, EventArgs e)
        {
            NotificationPopup.Visibility = Visibility.Collapsed;
            (Resources["HidePopup"] as Storyboard).Stop();
        }

        private void ShowMessage(string message)
        {
            NotificationPopup.Visibility = Visibility.Visible;
            NotificationPopup.Opacity = 1.0;

            MessageTextBox.Text = message;

            timer.Stop();
            timer.Start();
        }
    }
}
